package tabuvrp.gui;

import tabuvrp.core.Graph;
import tabuvrp.core.Solution;
import tabuvrp.core.stage.Stage;
import tabuvrp.core.stage.StageListener;
import tabuvrp.tabustage.TabuStage;
import tabuvrp.tabustage.BasicTabuStageParams;
import tabuvrp.optstage.OptStage;


public class TabuDemo extends javax.swing.JFrame implements StageListener {

    /** Creates new form TabuDemo */
    public TabuDemo(Graph graph) {
        this.graph = graph;
        n = graph.getNodeCount();
        mt = graph.getMTilde();
        initComponents();
        initS0();
        tabuStageDisplay1.clear();
        tabuStageDisplay2.clear();
        solutionDisplay1.setSolution(sol);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tabuStageDisplay1 = new tabuvrp.gui.TabuStageDisplay();
        tabuStageDisplay2 = new tabuvrp.gui.TabuStageDisplay();
        jPanel2 = new javax.swing.JPanel();
        optStageDisplay1 = new tabuvrp.gui.OptStageDisplay();
        runButton = new javax.swing.JButton();
        solutionDisplay1 = new tabuvrp.gui.SolutionDisplay(graph);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tabu Search VRP Demo");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED), "Tabu stages"));

        tabuStageDisplay1.setBorder(javax.swing.BorderFactory.createTitledBorder("stage 1"));

        tabuStageDisplay2.setBorder(javax.swing.BorderFactory.createTitledBorder("stage 2"));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabuStageDisplay1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tabuStageDisplay2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabuStageDisplay2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tabuStageDisplay1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED), "Optimizing stages"));

        optStageDisplay1.setBorder(javax.swing.BorderFactory.createTitledBorder("stage 3"));

        runButton.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        runButton.setText("Run");
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optStageDisplay1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, Short.MAX_VALUE)
                .addComponent(runButton)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(runButton)
                    .addComponent(optStageDisplay1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(solutionDisplay1, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(solutionDisplay1, javax.swing.GroupLayout.PREFERRED_SIZE, 690, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed
        runButton.setEnabled(false);
        initS0();
        tabuStageDisplay1.clear();
        tabuStageDisplay2.clear();
        optStageDisplay1.clear();
        solutionDisplay1.setSolution(sol);
        new Thread() {
            @Override
            public void run() {
                System.out.println("Stage 1 start");
                s0.runStage();
            }
        }.start();
    }//GEN-LAST:event_runButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private tabuvrp.gui.OptStageDisplay optStageDisplay1;
    private javax.swing.JButton runButton;
    private tabuvrp.gui.SolutionDisplay solutionDisplay1;
    private tabuvrp.gui.TabuStageDisplay tabuStageDisplay1;
    private tabuvrp.gui.TabuStageDisplay tabuStageDisplay2;
    // End of variables declaration//GEN-END:variables
    private final Graph graph;
    private final int n;
    private final double mt;
    private Solution sol;
    private BasicTabuStageParams s0params;
    private BasicTabuStageParams s1params;
    private TabuStage s0;
    private TabuStage s1;
    private OptStage s2;

        public void stageStarted(Stage stage) {
    }

    public void stepDone(Stage stage) {
    }

    public void newBestSolution(Stage stage) {
        solutionDisplay1.setSolution(stage.getBestSolution());
//        try {
//            java.util.concurrent.TimeUnit.MILLISECONDS.sleep(800);
//        } catch (Exception exc) {}
    }

    public void stageStopped(Stage stage) {
        System.out.println(stage.getBestSolution() + "\n");
        if (stage == s0) {
            initS1();
            new Thread() {
                @Override
                public void run() {
                    System.out.println("Stage 2 start");
                    s1.runStage();
                }
            }.start();
        }
        else if (stage == s1) {
            initS2();
            new Thread() {
                @Override
                public void run() {
                    System.out.println("Stage 3 start");
                    s2.runStage();
                }
            }.start();
        }
        else if (stage == s2) {
            System.gc();
            runButton.setEnabled(true);
        }
    }

    private void initS0() {
        /* SOLUTION INIT */
        sol = new Solution(graph);
        for(int i = 1; i < n; ++i) {
            sol.makePath(i);
        }
        /* STAGE 0 */
        s0params = new BasicTabuStageParams(sol,
                    0.5, 9,                                // alpha, beta
                    (int) Math.min(n, 5),                   // p
                    (int) Math.min(n, Math.round(5 * mt)),  // q
                    7, 9,                                   // min theta, max theta
                    10 * n);

        s0 = new TabuStage(graph, s0params, sol);
        s0.addTabuStageListener(tabuStageDisplay1);
        s0.addStageListener(this);
    }


    private void initS1() {
        /* STAGE 1 */
        sol = s0.getBestSolution();
        s1params = new BasicTabuStageParams(sol,
                    2, 5,
                    n/4,
                    n,
                    3, 6,
                    5 * n);
        s1 = new TabuStage(graph, s1params, sol);
        s1.addTabuStageListener(tabuStageDisplay2);
        s1.addStageListener(this);
    }

    private void initS2() {
        /* STAGE 2 */
        sol = s1.getBestSolution();
        s2 = new OptStage(graph, sol);
        s2.addStageListener(optStageDisplay1);
        s2.addStageListener(this);
    }
}
